BUILD_VERSION?=1.1.2

BUILD_NAME:=remote
BUILD_DATE:=$(shell date '+%Y%m%d.%H%M%S')
BUILD_DIR:=build
DEST_DIR:=dest
SRC_FILE:=*.go

ifdef IS_RELEASE
	# need IS_RELEASE=true
	SOFT_VERSION:=$(BUILD_VERSION)
else
	SOFT_VERSION:=$(BUILD_VERSION)-$(BUILD_DATE)
endif

UPX := $(shell which upx)
GIT := $(shell which git)

LDFLAGS:=-ldflags "-s -w -X remote_exec/util.VERSION=$(SOFT_VERSION)"

.PHONY: all clean prepare test win_amd_x86 win_amd_x64 win_arm_x64 linux_amd linux_arm package

all:clean win_amd_x86 win_amd_x64 win_arm_x64 linux_amd linux_arm package

build:clean win_amd_x86 win_amd_x64 win_arm_x64 linux_amd linux_arm package

release:clean win_amd_x86 win_amd_x64 win_arm_x64 linux_amd linux_arm package git_tag

prepare:
	@echo "====> prepare build environment"
	- mkdir $(DEST_DIR) $(BUILD_DIR)
	go mod tidy

test: prepare
	@echo "====> run unit case"
	- go test ./...

win_amd_x86: prepare test
	@echo "====> build windows amd32"
	export GO111MODULE=on \
		&& export CGO_ENABLED=0 \
		&& export GOOS=windows \
		&& export GOARCH=386 \
		&& go build $(LDFLAGS) -o $(BUILD_DIR)/$(BUILD_NAME)_windows_amd32.exe $(SRC_FILE)
ifneq ($(UPX),)
	upx $(BUILD_DIR)/$(BUILD_NAME)_windows_amd32.exe
endif

win_amd_x64: prepare test
	@echo "====> build windows amd64"
	export GO111MODULE=on \
		&& export CGO_ENABLED=0 \
		&& export GOOS=windows \
		&& export GOARCH=amd64 \
		&& go build $(LDFLAGS) -o $(BUILD_DIR)/$(BUILD_NAME)_windows_amd64.exe $(SRC_FILE)
ifneq ($(UPX),)
	upx $(BUILD_DIR)/$(BUILD_NAME)_windows_amd64.exe
endif

win_arm_x64: prepare test
	@echo "====> build windows arm64"
	export GO111MODULE=on \
		&& export CGO_ENABLED=0 \
		&& export GOOS=windows \
		&& export GOARCH=arm64 \
		&& go build $(LDFLAGS) -o $(BUILD_DIR)/$(BUILD_NAME)_windows_arm64.exe $(SRC_FILE)

linux_amd: prepare test
	@echo "====> build linux amd64"
	export GO111MODULE=on \
		&& export CGO_ENABLED=0 \
		&& export GOOS=linux \
		&& export GOARCH=amd64 \
		&& go build $(LDFLAGS) -o $(BUILD_DIR)/$(BUILD_NAME)_linux_amd64 $(SRC_FILE)
ifneq ($(UPX),)
	upx $(BUILD_DIR)/$(BUILD_NAME)_linux_amd64
endif

linux_arm: prepare test
	@echo "====> build linux arm64"
	export GO111MODULE=on \
		&& export CGO_ENABLED=0 \
		&& export GOOS=linux \
		&& export GOARCH=arm64 \
		&& go build $(LDFLAGS) -o $(BUILD_DIR)/$(BUILD_NAME)_linux_arm64 $(SRC_FILE)
ifneq ($(UPX),)
	upx $(BUILD_DIR)/$(BUILD_NAME)_linux_arm64
endif

package :
	@echo "====> archive artifact"
	cp config.yaml $(BUILD_DIR)/
	cp command.yaml $(BUILD_DIR)/
	cp README.md $(BUILD_DIR)/
	cp remote.sh $(BUILD_DIR)/
	cp remote.bat $(BUILD_DIR)/
	cd $(BUILD_DIR) && zip $(BUILD_NAME)-linux-$(SOFT_VERSION).zip * -x $(BUILD_NAME)_windows_* *.bat
	mv $(BUILD_DIR)/$(BUILD_NAME)-linux-$(SOFT_VERSION).zip $(DEST_DIR)/
	cd $(BUILD_DIR) && zip $(BUILD_NAME)-windows-$(SOFT_VERSION).zip * -x $(BUILD_NAME)_linux_* *.sh
	mv $(BUILD_DIR)/$(BUILD_NAME)-windows-$(SOFT_VERSION).zip $(DEST_DIR)/

git_tag:
ifdef IS_RELEASE
ifneq ($(GIT),)
	@echo "git tag $(SOFT_VERSION)"
	git tag -a $(SOFT_VERSION) -m $(SOFT_VERSION)
	git push origin $(SOFT_VERSION)
endif
endif

clean:
	@echo "====> clean temporary"
	- rm -rf $(BUILD_DIR)
	- rm -rf $(DEST_DIR)
	go clean